﻿@using KL.Store.Core
@{
    ViewBag.Title = "Çoklu Stok İşlemleri";
    Layout = "~/Views/Shared/_LayoutReportPanel.cshtml";
}
<div id="processResult"></div>
<br />
<div class="row">
    <div class="col-md-6">
        <div class="input-group2">
            <input type="text" name="searchProductPanel" id="searchProductPanel" class="form-control" placeholder="Aranacak terimi yazın...">
            <div id="productsearch_search_resultsPanel" class="window productsearchviewPanel" style="display: none;">
                <div id="divProductSearchResultPanel" style="padding: 6px;">
                </div>
            </div>
        </div>
    </div>
</div>
<div class="row">
    <div class="col-xs-12" id="productSearchResult">
        <br />
        <table class="table table-bordered">
            <thead>
                <tr>
                    <td style="vertical-align: middle; cursor: pointer;">Stok Adı</td>
                    <td style="width: 7%; vertical-align: middle;">Ücretsiz Kargo</td>
                    <td style="width: 7%; vertical-align: middle;">Hızlı Kargo</td>
                    <td style="width: 7%; vertical-align: middle;">Yeni Ürün</td>
                    <td style="width: 7%; vertical-align: middle;">Fırsat Ürünü</td>
                    <td style="width: 7%; vertical-align: middle;">Kampanyalı Ürün</td>
                    <td style="width: 7%; vertical-align: middle;">Kampanyalı Göster</td>
                    <td style="width: 7%; vertical-align: middle;">Stok Tut</td>
                    <td style="width: 7%; vertical-align: middle;">Hediye Metni</td>
                    <td style="width: 7%; vertical-align: middle;">Özel Metin</td>
                    <td style="width: 4%; text-align: center; vertical-align: middle;">
                        <input type="checkbox" name="SelectAll" class="SelectAll" id="SelectAll" title="Tümünü Seç" /></td>
                </tr>
            </thead>
            <tbody>
            </tbody>
        </table>
    </div>
</div>
<div class="row">
    <div class="col-xs-12">
        <hr />
        <h4>Seçilenlere Uygula;</h4>
        <div class="row">
            <div class="form-group">
                <div class="col-xs-4 col-sm-3 col-md-2">
                    <select name="UygulaIslemSec" id="UygulaIslemSec" class="form-control UygulaIslemSec" style="width: 100%;">
                        <option value="">Seçiniz</option>
                        <option value="UCRETSIZKARGO">Ücretsiz Kargo</option>
                        <option value="HIZLIKARGO">Hızlı Kargo</option>
                        <option value="YENIURUN">Yeni Ürün</option>
                        <option value="FIRSATURUN">Fırsat Ürünü</option>
                        <option value="KAMPANYALIURUN">Kampanyalı Ürün</option>
                        <option value="KAMPANYALIGOSTER">Kampanyalı Göster</option>
                        <option value="STOKTUT">Stok Tut</option>
                        <option value="GIFTSTRING">Hediye Metni</option>
                        <option value="OZELMETIN">Özel Metin</option>
                    </select>
                </div>
                <div class="col-xs-4 col-sm-3 col-md-2 UygulaIslemTF">
                    <select name="UygulaIslemTF" id="UygulaIslemTF" class="form-control" style="width: 100%;">
                        <option value="">Seçiniz</option>
                        <option value="1">Evet</option>
                        <option value="0">Hayır</option>
                    </select>
                </div>
                <div class="col-xs-4 col-sm-3 col-md-2 UygulaIslemEH">
                    <select name="UygulaIslemTF" id="UygulaIslemEH" class="form-control" style="width: 100%;">
                        <option value="">Seçiniz</option>
                        <option value="E">Evet</option>
                        <option value="H">Hayır</option>
                    </select>
                </div>
                <div class="col-xs-4 col-sm-3 col-md-2 UygulaIslemText">
                    <input name="UygulaIslemText" id="UygulaIslemText" class="form-control" style="width: 100%;" />
                </div>
                <div class="col-xs-4 col-sm-3 col-md-2 UygulaIslemPost">
                    <button type="button" class="btn btn-primary" onclick="IslemUygula();" id="UygulaIslemPost" disabled="disabled">Uygula</button>
                </div>
                <div class="clearfix"></div>
            </div>
        </div>
    </div>
</div>
<script>
    $('.editLiveSearch').live('click', function ()
    {
        if ($('#Product' + $(this).data('id')).length <= 0)
        {
            $.post('/ReportPanel/GetProductForEdit', { ProductId: $(this).data('id') }, function (result)
            {
                if (result == 'fail')
                {
                    hataMesaji('Ürün çağırılırken bir hata oluştu.');
                }
                else
                {
                    searchPanel.hide();
                    $('#productSearchResult table tbody').append(result);
                }
            });
        }
        else
        {
            hataMesaji('Bu ürün zaten seçilmiş.');
        }
        return false;
    });
    function GetSearchProductPanel()
    {
        var myText = $("#searchProductPanel").val();
        $.ajax({
            url: '/Search/LiveSearchFull?search=' + myText,
            type: 'POST',
            success: function (result)
            {
                $("#divLoadingGif").css("display", "none");
                var myProductList = $(result);
                var myHtlmString = '';
                $.each(myProductList[0].LiveSearch, function (key, val)
                {
                    myHtlmString += '<li><a href="javascript:void(0);" data-id="' + val.ProductId + '" class="editLiveSearch">' + val.ProductId + ' - ' + val.Barcode + ' - ' + val.ProductName + '</a></li>';
                });
                myHtlmString = '<ul id="live_productsearch_resultsPanel">' + myHtlmString + '</ul><div class="clearfix"></div>';
                $("#divProductSearchResultPanel").html(myHtlmString);
            }
        });
    }
    var searchPanel = (function ()
    {
        var builderPanel = function ()
        {
            $(document).ready(function ()
            {
                var lastSearched = "";
                var object = $('#searchProductPanel', $('.window.productsearchviewPanel').hide().parent()).focus(function ()
                {
                    this.active = true;
                    var value = $.trim(this.value);
                    if (value == object.data('empty'))
                    {
                        object.val('').parent().addClass('focus');
                    } else if (value.length >= 3) { searchPanel.show(); }
                }).blur(function ()
                {
                    this.active = false;

                    $(this.parentNode).removeClass('focus');
                    if ($.trim(object.val()) == '') { object.val(object.data('empty')); }
                }).keyup(function (e)
                {
                    if (e.which != 38 && e.which != 40 && e.which != 13 && e.which != 27 && lastSearched != $('#searchProductPanel').val())
                    {
                        lastSearched = $('#searchProductPanel').val();

                        $("#divLoadingGif").css("display", "block");
                        if (object.val().length >= 4)
                        {
                            GetSearchProductPanel();
                            searchPanel.show();
                        }
                        else
                        {
                            searchPanel.hide();
                        }
                        $(this.parentNode).removeClass('focus');
                    }
                });


                var parent = object.parents('.container').hover(function () { this.active = true; }, function ()
                {
                    this.active = false;
                    if (!object.prop('active')) { searchPanel.hide(); }
                });
            });
        }
        builderPanel.prototype = {
            busy: false,
            show: function () { $('.window.productsearchviewPanel').stop(true, true).animate({ opacity: 'show', height: 'show' }, 'fast'); },
            hide: function () { $('.window.productsearchviewPanel').stop(true, true).animate({ opacity: 'hide', height: 'hide' }, 'fast'); }
        };

        return new builderPanel();
    })();
    var headerElement = $('.input-group2');
    headerElement.find('input#searchProductPanel').keyup(function (e)
    {
        var ls = headerElement.find('#productsearch_search_resultsPanel');
        var lc = headerElement.find('#productsearch_search_resultsPanel #divProductSearchResultPanel #live_productsearch_resultsPanel li').size();
        if (lc > 0)
        {
            var newSelected = null;
            selectedSize = headerElement.find('#productsearch_search_resultsPanel li a.selected').size();
            selectedItem = headerElement.find('#productsearch_search_resultsPanel li a.selected');
            if (e.which == 40)
            {
                //down arrow key
                if (selectedSize <= 0)
                {
                    headerElement.find('#productsearch_search_resultsPanel li:first-child a').addClass('selected');
                }
                else
                {
                    newSelected = headerElement.find('#productsearch_search_resultsPanel li a.selected').parent().next().find('a');
                    headerElement.find('#productsearch_search_resultsPanel li a').removeClass('selected');
                    newSelected.addClass('selected');
                }
                return false;
            }
            else if (e.which == 38)
            {
                //up arrow key
                if (selectedSize <= 0)
                {
                    headerElement.find('#productsearch_search_resultsPanel li:last-child a').addClass('selected');
                }
                else
                {
                    newSelected = headerElement.find('#productsearch_search_resultsPanel li a.selected').parent().prev().find('a');
                    headerElement.find('#productsearch_search_resultsPanel li a').removeClass('selected');
                    newSelected.addClass('selected');
                }
                return false;
            }
            else if (e.which == 13)
            {
                //enter key
                if (selectedSize <= 0)
                {
                }
                else
                {
                    selectedItem.click();
                }
            }
            else if (e.which == 27)
            {
                searchPanel.hide();
            }
        }
    });
    $('#SelectAll').live('change', function ()
    {
        if ($(this).is(':checked'))
        {
            $('.SelectedProduct').attr('checked', 'checked');
            $(this).attr('title', 'Seçimleri Kaldır');
        }
        else
        {
            $('.SelectedProduct').removeAttr('checked');
            $(this).attr('title', 'Tümünü Seç');
        }
    });
    $('#UygulaIslemSec').live('change', function ()
    {
        var sec = $(this);
        var value = sec.val();
        if (value == '')
        {
            $('.UygulaIslemTF').hide(0);
            $('.UygulaIslemEH').hide(0);
            $('.UygulaIslemText').hide(0);
            $('.UygulaIslemPost').hide(0);

            $('#UygulaIslemTF').val('');
            $('#UygulaIslemEH').val('');
            $('#UygulaIslemText').val('');
            $('#UygulaIslemPost').attr('disabled', 'disabled');
        }
        else if (value == 'UCRETSIZKARGO' || value == 'HIZLIKARGO' || value == 'YENIURUN' || value == 'FIRSATURUN' || value == 'KAMPANYALIURUN' || value == 'KAMPANYALIGOSTER')
        {
            $('.UygulaIslemTF').show(0);
            $('.UygulaIslemEH').hide(0);
            $('.UygulaIslemText').hide(0);
            $('.UygulaIslemPost').show(0);

            $('#UygulaIslemTF').val('');
            $('#UygulaIslemEH').val('');
            $('#UygulaIslemText').val('');
            $('#UygulaIslemPost').attr('disabled', 'disabled');
        }
        else if (value == 'STOKTUT')
        {
            $('.UygulaIslemTF').hide(0);
            $('.UygulaIslemEH').show(0);
            $('.UygulaIslemText').hide(0);
            $('.UygulaIslemPost').show(0);

            $('#UygulaIslemTF').val('');
            $('#UygulaIslemEH').val('');
            $('#UygulaIslemText').val('');
            $('#UygulaIslemPost').attr('disabled', 'disabled');
        }
        else if (value == 'GIFTSTRING' || value == 'OZELMETIN')
        {
            $('.UygulaIslemTF').hide(0);
            $('.UygulaIslemEH').hide(0);
            $('.UygulaIslemText').show(0);
            $('.UygulaIslemPost').show(0);

            $('#UygulaIslemTF').val('');
            $('#UygulaIslemEH').val('');
            $('#UygulaIslemText').val('');
            $('#UygulaIslemPost').removeAttr('disabled');
        }
    });
    $('#UygulaIslemTF').live('change', function ()
    {
        if ($(this).val() == '')
        {
            $('#UygulaIslemPost').attr('disabled', 'disabled');
        }
        else
        {
            $('#UygulaIslemPost').removeAttr('disabled');
        }
    });
    $('#UygulaIslemEH').live('change', function ()
    {
        if ($(this).val() == '')
        {
            $('#UygulaIslemPost').attr('disabled', 'disabled');
        }
        else
        {
            $('#UygulaIslemPost').removeAttr('disabled');
        }
    });
    function IslemUygula()
    {
        var checkedCount = $('.SelectedProduct:checked').length;
        var completed = 0;
        var notcompleted = 0;
        var secim = $('#UygulaIslemSec').val();
        var changeThis = '';
        var changedValue = '';
        var changedType = '';
        switch (secim)
        {
            case 'UCRETSIZKARGO': changeThis = 'FreeCargoField'; changedValue = $('#UygulaIslemTF').val(); changedType = 'TF'; break;
            case 'HIZLIKARGO': changeThis = 'FastCargoField'; changedValue = $('#UygulaIslemTF').val(); changedType = 'TF'; break;
            case 'YENIURUN': changeThis = 'NewProductField'; changedValue = $('#UygulaIslemTF').val(); changedType = 'TF'; break;
            case 'FIRSATURUN': changeThis = 'OpportunityProductField'; changedValue = $('#UygulaIslemTF').val(); changedType = 'TF'; break;
            case 'KAMPANYALIURUN': changeThis = 'CampaignProductField'; changedValue = $('#UygulaIslemTF').val(); changedType = 'TF'; break;
            case 'KAMPANYALIGOSTER': changeThis = 'ShowAsCampaignProductField'; changedValue = $('#UygulaIslemTF').val(); changedType = 'TF'; break;
            case 'STOKTUT': changeThis = 'CheckStockField'; changedValue = $('#UygulaIslemEH').val(); changedType = 'EH'; break;
            case 'GIFTSTRING': changeThis = 'GiftStringField'; changedValue = $('#UygulaIslemText').val(); changedType = 'Text'; break;
            case 'OZELMETIN': changeThis = 'SpecialTextField'; changedValue = $('#UygulaIslemText').val(); changedType = 'Text'; break;
            default: changeThis = ''; changedValue = ''; changedType = ''; break;
        }
        if (checkedCount > 0)
        {
            if (secim != '' && confirm('Tüm seçili kayıtlar güncellenecek, emin misiniz?'))
            {
                loadingMsg();
                $('.SelectedProduct:checked').each(function ()
                {
                    var id = $(this).data('id');
                    $.post('/ReportPanel/StokFiltreSet', {
                        ProductId: id,
                        Field: secim,
                        ValueTF: $('#UygulaIslemTF').val(),
                        ValueEH: $('#UygulaIslemEH').val(),
                        ValueText: $('#UygulaIslemText').val()
                    }, function (result)
                    {
                        //successed
                        if (result == 'fail')
                        {
                            notcompleted++;
                        }
                        else
                        {
                            completed++;
                            if (changedType == 'TF')
                            {
                                if (changedValue == '1')
                                {
                                    $('#Product' + id).find('.' + changeThis).html('<b>Evet</b>');
                                }
                                else
                                {
                                    $('#Product' + id).find('.' + changeThis).html('Hayır');
                                }
                            }
                            if (changedType == 'EH')
                            {
                                if (changedValue == 'E')
                                {
                                    $('#Product' + id).find('.' + changeThis).html('<b>Evet</b>');
                                }
                                else
                                {
                                    $('#Product' + id).find('.' + changeThis).html('Hayır');
                                }
                            }
                            if (changedType == 'Text')
                            {
                                if (changedValue != '')
                                {
                                    $('#Product' + id).find('.' + changeThis).html('<b>Dolu</b>');
                                }
                                else
                                {
                                    $('#Product' + id).find('.' + changeThis).html('Boş');
                                }
                            }

                        }

                        if (completed == checkedCount)
                        {
                            $('#processResult').show(0).html('<div class="bg-info"><p><i class="fa fa-info-circle"></i>&nbsp;Bilgiler Kaydedildi</p></div>').delay(5000).hide(0);
                        }
                        else if (completed + notcompleted == checkedCount)
                        {
                            hataMesaji('İşlem tamamlandı ancak bazı stokların güncellenmesi sırasında hata oluştu. Yeniden filtreleyerek kontrol ediniz.');
                        }
                    });

                    //alert('aqlattın :( -- ' + id);
                });
            }
        }
        else
        {
            hataMesaji('Hiç ürün seçilmemiş.');
        }
    }
</script>
